package GUI;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import ModelLayer.PartSale;
import ModelLayer.Sale;

//handle partsales
public class SalesHistoryTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
		
	private ArrayList<?> container;
	private String[] columnNames;
		
	public SalesHistoryTableModel(String[] newSaleColumnNames, ArrayList<Sale> salesList) {
		
		this.container = salesList;         
		this.columnNames = newSaleColumnNames;			
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return container.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Sale s = (Sale) container.get(rowIndex);
		String linieSalg = "LinieSalg";
			
		//"Kunde", "Ansat", "Total prisen", "Dato", "Salgs Invoice nummer"
		if(columnIndex == 0) {
			try {
				if (s.getCustomer().getName() != null) {
					return s.getCustomer().getName();
				}
				else {
					return linieSalg;
				}
			}
			catch(NullPointerException e) {
				
			}
		} 
			
		if(columnIndex == 1) {
			return s.getEmployee().getName();
		}
			
		if(columnIndex == 2) {
			return s.getTotalPrice();
		}
			
		if(columnIndex == 3) {
			return s.getDate();
		}
			
		if(columnIndex == 4) {
			return s.getSaleInvoiceNumber();
		}
		
			
		return null;
	}
		
	@Override
     public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
     }   
		
	@Override
	public String getColumnName(int index) {
		return columnNames[index];
	}	
}

	


